#include "DockElement.h"

#include "../rs/noimage.h"

DockElement::DockElement(const ElementType& type_) : type (type_),
													 parent (0)
{
}

DockElement::DockElement(const ElementType& type_, DockElement * parent_) : type (type),
																			parent(parent_)
{}

DockElement::DockElement(const ElementType& type_, DockElement * parent_, std::vector<DockElement*> children_) : type (type_),
																												 parent(parent),
																												 children(children_)
{}



DockElement::~DockElement()
{	
	std::vector<DockElement*>::iterator it;
	
	for ( it = children.begin() ; it < children.end(); it++ )
	{
		delete *it;
	}

	children.clear();
}

DockElement * DockElement::DockElementFactoryPizzaFactory(String type_)
{
	if(type_ == T("separator"))
	{
		DBG("separator");
	}
	return 0;
}

const ElementType& DockElement::getType()
{
	return type;
}

const String DockElement::getImgURL()
{
	return imageURL;
}
	
DockElement* DockElement::getParent()
{
	return parent;
}
	
std::vector<DockElement*>& DockElement::getChildren()
{
	return children;
}

DockElement * DockElement::getChild(const int& index)
{
	if(index < 0 || index >= (int) children.size())
		return 0;

	return children[index];
}

bool DockElement::hasChildren()
{
	return (children.size() > 0);
}

void DockElement::setParent(DockElement * parent_)
{
	parent = parent_;
}

void DockElement::addChild(DockElement * child)
{
	children.push_back(child);
	child->setParent(this);
}

Image * DockElement::getImageToDraw()
{
	String basePath = File::getCurrentWorkingDirectory().getFullPathName();
	String toImagePath = "/res/" + imageURL;
	String thePath = basePath + toImagePath;

	Image * tmp = ImageFileFormat::loadFrom(File(thePath));
	
	if(!tmp)
	{
		tmp = ImageFileFormat::loadFrom(noimage::file_broken_png, noimage::file_broken_pngSize);
		/*
		needsDefaultImage();
		return elementImage;
		*/
	}

	int w = tmp->getWidth();
	int h = tmp->getHeight();

	elementImage = new Image(Image::ARGB, w, h + 3 + h/2, true);

	/*
	int lineStride = 0, dummy;
	const uint8 * sd = tmp->lockPixelDataReadOnly(0,0, w, h, lineStride, dummy);
	elementImage->setPixelData(0, 0, w, h, sd, lineStride);
	tmp->releasePixelDataReadOnly(sd);
*/

	for(int x = 0; x < w; x++)
		for(int y = 0; y < h; y++)
			elementImage->setPixelAt(x, y, tmp->getPixelAt(x, y));

	int nh = elementImage->getHeight();
	for(int x = 0; x < w; x++)
		for(int y = 0; y < h / 2; y++)

		{
			elementImage->setPixelAt(x, y + 3 + h, tmp->getPixelAt(x, h - y));
			float alpha = 1.0f - 2.0f * (float) y / (float) h;
			elementImage->multiplyAlphaAt(x, y + 3 + h, alpha);
		}

	delete tmp;
	return elementImage;
}

String DockElement::getTitle()
{
	return title;
}

void DockElement::needsDefaultImage()
{
	// create a transparent 500x500 image..
    Image * myImage = new Image(Image::ARGB, 128, 128, true);

    Graphics g (*myImage);
    g.setColour (Colours::red);
    g.fillEllipse (0, 0, 128, 128);  // draws a red ellipse in our image.

	elementImage = myImage;
}